package xyz.xxin.xat.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;

import xyz.xxin.xat.R;
import xyz.xxin.xat.utils.DownloadUtils;

public class BreakPointActivity extends AppCompatActivity implements DownloadUtils.DownloadListener {

    private ProgressBar progressBar;
    private Button downloadBtn;
    private Button pauseDownload;
    private TextView progressText;

    private boolean isPaused = false;           // 是否暂停
    private boolean isDownload = false;         // 是否下载

    private DownloadUtils downloadUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_break_point);
        progressBar = findViewById(R.id.progressBar);
        downloadBtn = findViewById(R.id.downloadBtn);
        pauseDownload = findViewById(R.id.pauseDownload);
        progressText = findViewById(R.id.progressText);

        // 开始下载
        downloadBtn.setOnClickListener(view -> {
            if (isDownload) {
                downloadUtils.cancelDownload();
                downloadBtn.setText("开始下载");
            } else {
                downloadUtils = new DownloadUtils(this);
                downloadUtils.execute("https://me.xxin.xyz/file/test.apk",
                        Environment.getExternalStorageDirectory().getAbsolutePath(), "file.apk");
                downloadBtn.setText("取消下载");
            }
            isDownload = !isDownload;
        });

        // 暂停下载
        pauseDownload.setOnClickListener(view -> {
            if (isDownload){
                if (isPaused) {
                    downloadUtils = new DownloadUtils(this);
                    downloadUtils.execute("https://me.xxin.xyz/file/test.apk",
                            Environment.getExternalStorageDirectory().getAbsolutePath(), "file.apk");
                    pauseDownload.setText("暂停下载");
                } else {
                    downloadUtils.pauseDownload();
                    pauseDownload.setText("继续下载");
                }
                isPaused = !isPaused;
            } else {
                Toast.makeText(this, "还没有开始，怎么暂停？", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onProgress(int progress) {
        progressText.setText("下载进度:" + progress + "%");
        progressBar.setProgress(progress);
    }

    @Override
    public void onSuccess(File downloadCacheFile) {
        Toast.makeText(this, "下载成功: " + downloadCacheFile, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailed() {
        Toast.makeText(this, "下载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPaused() {
        Toast.makeText(this, "下载暂停", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCanceled() {
        progressText.setText("下载进度:0%");
        progressBar.setProgress(0);
        Toast.makeText(this, "下载取消", Toast.LENGTH_SHORT).show();
    }
}